Skip to content

Python: fix per-run additional_beta_flags leaking into Anthropic request kwargs#7060

Merged
eavanvalkenburg merged 2 commits into
microsoft:mainfrom
albatrossflyon-coder:fix/anthropic-additional-beta-flags-leak
Jul 14, 2026
Merged

Python: fix per-run additional_beta_flags leaking into Anthropic request kwargs#7060
eavanvalkenburg merged 2 commits into
microsoft:mainfrom
albatrossflyon-coder:fix/anthropic-additional-beta-flags-leak

Conversation

@albatrossflyon-coder

Copy link
Copy Markdown
Contributor

Summary

Fixes #5764. _prepare_options in agent_framework_anthropic/_chat_client.py copies every key from the caller-supplied options dict into run_options, excluding only instructions and response_format. A per-run additional_beta_flags value is correctly folded into the final betas set by _prepare_betas, but the raw additional_beta_flags key itself was never excluded from run_options, so it survived and was forwarded straight through to AsyncMessages.create(**run_options):

TypeError: AsyncMessages.create() got an unexpected keyword argument 'additional_beta_flags'

Fix

Add additional_beta_flags to the exclusion set alongside the other framework-level keys already handled separately (instructions, response_format).

Note on prior attempt

#5772 attempted the same fix but was closed after CI failures on that branch (per the maintainer comments, it wasn't closed on the merits of the approach — the contributor just never rebased/reopened). This PR re-implements the same direction cleanly against current main with a regression test.

Test plan

  • Added test_prepare_options_consumes_additional_beta_flags, which fails on main (the key is forwarded and would raise TypeError at the real API boundary) and passes with the fix
  • pytest packages/anthropic/tests/test_anthropic_client.py -k "additional_beta_flags or prepare_options" — 24 passed
  • pytest packages/anthropic/tests/ (full package) — passes, no new failures

…est kwargs

_prepare_options copied every key from the caller-supplied options dict
into run_options except "instructions" and "response_format". A
per-run additional_beta_flags value is correctly folded into the betas
set by _prepare_betas, but the raw key was never excluded, so it
survived into run_options and was forwarded straight through to
AsyncMessages.create(), which rejects it with TypeError: got an
unexpected keyword argument 'additional_beta_flags'. Add it to the
exclusion set alongside the other framework-level keys.

Fixes microsoft#5764
Copilot AI review requested due to automatic review settings July 10, 2026 18:58
@giles17 giles17 added the python Usage: [Issues, PRs], Target: Python label Jul 10, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes an Anthropic client integration bug where per-run additional_beta_flags could leak into the raw request kwargs passed to AsyncMessages.create(...), causing a TypeError. The change keeps additional_beta_flags framework-scoped (consumed into betas) rather than forwarding it as an unexpected parameter.

Changes:

  • Exclude additional_beta_flags from the run_options kwargs forwarded to the Anthropic SDK.
  • Add a regression test ensuring additional_beta_flags is consumed into betas and not forwarded raw.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
python/packages/anthropic/agent_framework_anthropic/_chat_client.py Filters additional_beta_flags out of forwarded request kwargs when building run_options.
python/packages/anthropic/tests/test_anthropic_client.py Adds a regression test asserting additional_beta_flags is not forwarded and is included in betas.

Comment thread python/packages/anthropic/agent_framework_anthropic/_chat_client.py
Copilot's review on the original fix pointed out the exclusion only
covered the options-dict copy, not kwargs passed directly to
_prepare_options — so additional_beta_flags supplied as a raw kwarg
would still leak through and reproduce the same TypeError. Add the
same exclusion to filtered_kwargs for consistency, with a regression
test covering the kwarg path.
@albatrossflyon-coder

Copy link
Copy Markdown
Contributor Author

Good catch — fixed in 1860079. additional_beta_flags is now also excluded from filtered_kwargs, so it's dropped consistently whether it arrives via the options dict or as a direct kwarg. Added a regression test for the kwarg path (test_prepare_options_drops_additional_beta_flags_passed_as_kwarg).

@albatrossflyon-coder

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree, send. I suppose this is the way for the CLA because I couldn't find a button up there I let me know thank you it's a pleasure

@albatrossflyon-coder

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

@github-actions

Copy link
Copy Markdown
Contributor

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/anthropic/agent_framework_anthropic
   _chat_client.py5173792%467, 470, 551, 672, 684, 772, 806, 896, 935–936, 1014, 1016, 1046–1047, 1092, 1108–1109, 1116–1118, 1122–1124, 1128–1131, 1248, 1258, 1310, 1458–1459, 1476, 1489, 1502, 1527–1528
TOTAL44138526388% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
8853 33 💤 0 ❌ 0 🔥 2m 16s ⏱️

@eavanvalkenburg

Copy link
Copy Markdown
Member

@albatrossflyon-coder I think you need to recomment the cla approval...

@albatrossflyon-coder

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

@eavanvalkenburg
eavanvalkenburg added this pull request to the merge queue Jul 14, 2026
Merged via the queue into microsoft:main with commit b5300fe Jul 14, 2026
37 of 38 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python Anthropic: per-run additional_beta_flags leaks into request kwargs

6 participants